﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ActionSystem.Core.PhysicsLogic
{
    public class BasePhysicsAbility
    {
        BaseStateMachine stateMachine;
        public BasePhysicsAbility(BaseStateMachine baseStateMachine)
        {
            stateMachine = baseStateMachine;
        }

        public virtual void PreSimulationUpdate(float fixedDeltaTime) 
        {
            //执行当前状态的逻辑
            var currentState = stateMachine.CurrentState;
            var currentTime = stateMachine.CurrentStateData;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IPhysicsActionHandler)?.BeforePhysicsSimulate(action, currentTime.logicDeltaTime,fixedDeltaTime);
            }
            
            
            //判断是否需要执行下一个状态
            var nextState = stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = stateMachine.NextStateData;
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IPhysicsActionHandler)?.BeforePhysicsSimulate(action, nextTime.logicDeltaTime, fixedDeltaTime);
            }                        
        }
        public virtual void PostSimulationUpdate(float fixedDeltaTime) 
        {
            //执行当前状态的逻辑
            var currentState = stateMachine.CurrentState;
            var currentTime = stateMachine.CurrentStateData;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IPhysicsActionHandler)?.AfterPhysicsSimulate(action,currentTime.logicDeltaTime ,fixedDeltaTime);
            }
            
            
            //判断是否需要执行下一个状态
            var nextState = stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = stateMachine.NextStateData;
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IPhysicsActionHandler)?.AfterPhysicsSimulate(action, nextTime.logicDeltaTime, fixedDeltaTime);
            }                        
        }


    }

}
